<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>LOGIN</h1>

      telphone <input type="text" value="13412341234"><br>
      password <input type="password" value="234234"><br>
      <input type="button" id="loginBtn" value="登录">


  <script>
    let loginBtn=document.querySelector("#loginBtn");
    loginBtn.addEventListener('click',()=>{
        let telphone=document.querySelector("input[type=text]").value;
        let password=document.querySelector("input[type=password]").value;
        //对数据进行格式校验
       /* let noSpace=/^\s*$/;
        if(noSpace.test(telphone) || noSpace.test(password)){
            alert("信不能为空");
            return ;
        }

        let telRex=/^1[3-9]\d{9}$/;
        if(!telRex.test(telphone)){
            alert("手机号格式不正确");
            return ;
        }*/

        //发送ajax请求
        fetch("/user/login",{
            method:"POST",
            headers:{
                "Content-Type":"application/json"
            },
            body:JSON.stringify({
                telphone,
                password
            })
        }).then(res=>res.json()).then(data=>{
             if(!data.success){
                 alert(data.message);
             }else{
                 //说明登录成功
                 //1. 将用户存储到sessionStorage
                  sessionStorage.setItem("user",JSON.stringify(data.data));
                 //2. 跳转到首页
             }
        }).catch((e)=>{
            console.log(e);
        })
    })

  </script>
</body>
</html>